package com.loe.camera;

import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import androidx.fragment.app.FragmentActivity;

/**
 * 选择图片弹框
 *
 * @author zls
 * @since 2022/5/21-11:49
 */
public class TakePhotoDialog
{
    private BottomSheetDialog dialog;

    private boolean isInit = false;

    private ViewGroup rootView;

    private FragmentActivity activity;
    private PhotoConfig config;
    private LoeCamera.OnPathCallback onPathCallback;

    private String text;
    private Integer imageRes;

    private CameraImageView imageView;

    public CameraImageView getImageView()
    {
        return imageView;
    }

    public TakePhotoDialog(FragmentActivity activity)
    {
        this.dialog = new BottomSheetDialog(activity);

        this.activity = activity;
        config = new PhotoConfig();
    }

    public TakePhotoDialog(FragmentActivity activity, PhotoConfig config, LoeCamera.OnPathCallback onPathCallback)
    {
        this.dialog = new BottomSheetDialog(activity);

        this.activity = activity;
        this.config = config;
        this.onPathCallback = onPathCallback;
    }

    public TakePhotoDialog config(PhotoConfig config)
    {
        this.config = config;
        return this;
    }

    public TakePhotoDialog setOnPathCallback(LoeCamera.OnPathCallback onPathCallback)
    {
        this.onPathCallback = onPathCallback;
        return this;
    }

    public TakePhotoDialog image(int resId)
    {
        this.imageRes = resId;
        return this;
    }

    public TakePhotoDialog text(String text)
    {
        this.text = text;
        return this;
    }
    public TakePhotoDialog permissionTip(String tip)
    {
        config.setPermissionTip(tip);
        return this;
    }

    public void show()
    {
        if (!dialog.isShowing())
        {
            Window window = dialog.getWindow();
            if(!isInit && window != null)
            {
                dialog.setContentView(R.layout.camera_take_photo_dialog);
                window.setBackgroundDrawableResource(android.R.color.transparent);
                View bottom_sheet = dialog.getDelegate().findViewById(R.id.design_bottom_sheet);
                if(bottom_sheet != null)
                {
                    bottom_sheet.setBackgroundResource(android.R.color.transparent);
                }
                try
                {
                    BottomSheetBehavior<View> mBehavior = BottomSheetBehavior.from(window.findViewById(R.id.design_bottom_sheet));
                    mBehavior.setSkipCollapsed(true);
                    mBehavior.setPeekHeight(3000);
                    mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }catch (Exception e)
                {
                }
                rootView = (ViewGroup) window.getDecorView();
                rootView.setPadding(0, 0, 0, 0);
                isInit = true;
            }

            dialog.show();
            onShow();
        }
    }

    private void onShow()
    {
        imageView = rootView.findViewById(R.id.imageView);
        if(imageRes != null)
        {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(imageRes);
        }
        TextView textView = rootView.findViewById(R.id.textView);
        if(text != null)
        {
            textView.setVisibility(View.VISIBLE);
            textView.setText(text);
        }

        rootView.findViewById(R.id.buttonAlbum).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                LoeCamera.takePhotoAlbum(activity, config, onPathCallback);
                dialog.dismiss();
            }
        });

        rootView.findViewById(R.id.buttonCamera).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                LoeCamera.takePhotoSysCamera(activity, config, onPathCallback);
                dialog.dismiss();
            }
        });

        rootView.findViewById(R.id.buttonCancel).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                config.runError();
                dialog.dismiss();
            }
        });

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
        {
            @Override
            public void onCancel(DialogInterface dialogInterface)
            {
                config.runError();
                dialog.dismiss();
            }
        });
    }
}
